package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.entity.ResDate;
import com.itheima.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.vo.RptDateVo;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    /**
     * 默认查询一年的会员数量数据
     * @return
     */
    @Override
    public ResDate findAmounByYear() {

        List<RptDateVo> byYear = this.baseMapper.findAmounByYear();
        ArrayList<String> strings = new ArrayList<>();
        ArrayList<Integer> total = new ArrayList<>();
        Integer maxNum = 0;


        for (RptDateVo year : byYear) {
            String s = year.getFyear() + "-" + year.getFmonth();
            strings.add(s);
            total.add(year.getTotal());
            if (year.getTotal() > maxNum) {
                maxNum = year.getTotal();
            }
        }
        return new ResDate(strings, total, maxNum + 10);
    }


    /**
     * 查询你不同时间段的会员数量数据
     * @param start 开始日期
     * @param end 结束日期
     * @return
     */
    @Override
    public ResDate findDate(String start, String end) {

        String[] dateStart = start.split("-");
        StringBuffer startBuffer = new StringBuffer();
        for (String dateS : dateStart) {
            startBuffer.append(dateS);
        }
        String s = startBuffer.toString();
        System.out.println(s);

        String[] dateEnd = end.split("-");
        StringBuffer endBuffer = new StringBuffer();
        for (String dateE : dateEnd) {
            endBuffer.append(dateE);
        }
        String e = endBuffer.toString();
        System.out.println(e);

        //将新的数值放到对象内

        long s1 = Long.valueOf(s);
        long e1 = Long.valueOf(e);

        long num = e1 - s1;
        System.out.println(num);

        //判断做出 选择周
        if (num < 300 && num > 7) {
            List<RptDateVo> week = this.baseMapper.findDateByWeek(s1, e1);
            ArrayList<String> strings = new ArrayList<>();
            ArrayList<Integer> total = new ArrayList<>();
            Integer maxNum = 0;
            for (RptDateVo weekDate : week) {
                String weekNum = weekDate.getFmonth() + "月第" + weekDate.getFweek() + "周";
                strings.add(weekNum);
                total.add(weekDate.getTotal());
                if (weekDate.getTotal() > maxNum) {
                    maxNum = weekDate.getTotal();
                }
            }
            return new ResDate(strings, total, maxNum + 10);
        }
        //按月查询
        if (num > 300 && num < 15000) {
            List<RptDateVo> dateByMonth = this.baseMapper.findDateByMonth(s1, e1);
            ArrayList<String> strings = new ArrayList<>();
            ArrayList<Integer> total = new ArrayList<>();
            Integer maxNum = 0;
            for (RptDateVo monthDate : dateByMonth) {
                strings.add(monthDate.getFmonth()+"月");
                total.add(monthDate.getTotal());
                if (monthDate.getTotal() > maxNum) {
                    maxNum = monthDate.getTotal();
                }
            }
            return new ResDate(strings, total, maxNum + 10);
        }

        //按季度查询
        if (num > 15000 && num < 30000) {
            List<RptDateVo> dateByQuarter = this.baseMapper.findDateByQuarter(s1, e1);
            ArrayList<String> strings = new ArrayList<>();
            ArrayList<Integer> total = new ArrayList<>();
            Integer maxNum = 0;
            for (RptDateVo quarter : dateByQuarter) {
                String fQuarter = quarter.getFyear() + "年第" + quarter.getFquarter() + "季度";
                strings.add(fQuarter);
                total.add(quarter.getTotal());
                if (quarter.getTotal() > maxNum) {
                    maxNum = quarter.getTotal();
                }
            }
            return new ResDate(strings, total, maxNum + 10);
        }

        //按年查询
        if (num > 30000) {
            List<RptDateVo> dateByYear = this.baseMapper.findDateByYear(s1, e1);
            ArrayList<String> strings = new ArrayList<>();
            ArrayList<Integer> total = new ArrayList<>();
            Integer maxNum = 0;
            for (RptDateVo year : dateByYear) {
                String fYear= year.getFyear() + "年";
                strings.add(fYear);
                total.add(year.getTotal());
                if (year.getTotal() > maxNum) {
                    maxNum = year.getTotal();
                }
            }
            return new ResDate(strings, total, maxNum + 10);
        }
        return null;
    }




    @Override
    public Member fingByTelephone(String telephone) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone",telephone);
        return this.getOne(queryWrapper);
    }


}